package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.DishEntity;
import com.sky.entity.SetmealDishEntity;
import com.sky.entity.SetmealEntity;
import com.sky.enumeration.DishStatusEnum;
import com.sky.enumeration.SetmealStatusEnum;
import com.sky.exceptionHandler.ServiceException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public List<SetmealEntity> list(SetmealEntity entity) {
        List<SetmealEntity> list = setmealMapper.list(entity);
        return list;
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        SetmealEntity setmealEntity = new SetmealEntity();
        BeanUtils.copyProperties(setmealDTO, setmealEntity);
        setmealMapper.save(setmealEntity);
        Long id = setmealEntity.getId();
        log.info("保存套餐成功，套餐ID：{}", id);

        for (SetmealDishEntity dishEntity : setmealDTO.getSetmealDishes()) {
            dishEntity.setSetmealId(id);
        }

        setmealDishMapper.saveBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public List<SetmealEntity> page(SetmealPageDTO pageDTO, SetmealStatusEnum statusEnum) {
        if (pageDTO.getPage() == null) {
            pageDTO.setPage(1);
        }
        if (pageDTO.getPageSize() == null) {
            pageDTO.setPageSize(10);
        }
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getPageSize());
        List<SetmealEntity> list = setmealMapper.page(pageDTO, statusEnum);
        PageInfo<SetmealEntity> setmealEntityPageInfo = new PageInfo<>(list);
        return setmealEntityPageInfo.getList();
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        SetmealEntity setmealEntity = new SetmealEntity();
        BeanUtils.copyProperties(setmealDTO, setmealEntity);
        setmealMapper.update(setmealEntity);

        // 1. 先删除套餐对应的菜品
        setmealDishMapper.delBySetmealId(setmealEntity.getId());

        // 2. 将这次的菜品新增到套餐中
        for (SetmealDishEntity setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.saveBatch(setmealDTO.getSetmealDishes());
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        log.info("查询成功，套餐数据：{}", setmealVO);
        return setmealVO;
    }

    @Override
    public void startOrStop(SetmealStatusEnum statusEnum, Long id) {
        // 如果是起售，则判断套餐内是否有停售菜品
        if (statusEnum.equals(SetmealStatusEnum.Enable)) {
            List<DishEntity> dishEntityList = setmealDishMapper.getDishBySetmealIdWithDisabled(id);
            if (dishEntityList.size() > 0) {
                throw new ServiceException("起售的套餐中包含停售的菜品");
            }
        }

        SetmealEntity setmealEntity = SetmealEntity.builder().id(id).status(statusEnum).build();
        setmealMapper.update(setmealEntity);
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 起售中的套餐不能删除
        Integer count = setmealMapper.countBySetmealEnable(ids);
        log.info("起售中套餐数量=>{}", count);
        if (count > 0) {
            throw new ServiceException("起售中的套餐不能删除");
        }
        // 1. 先删除setmeal_dish中的菜品
        setmealDishMapper.deleteBatchBySetmealId(ids);
        // 2. 删除套餐
        setmealMapper.deleteBatch(ids);
    }
}
